//
//  SHComposeToolbarView.swift
//  SHWeibo
//
//  Created by jingming wang on 16/8/3.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

// 按钮枚举
enum SHComposeToolbarViewType: Int{
    // 图片
    case Picture = 1001
    // @
    case Mention = 1002
    // #
    case Trend = 1003
    // 表情
    case Emoticon = 1004
    // +
    case Add = 1005
}

class SHComposeToolbarView: UIView {
    // 定义一个闭包
    var closure:((type: SHComposeToolbarViewType) -> ())?
    // 表情按钮
    private lazy var emoticonButton: UIButton = UIButton()
    // 是否是表情键盘
    var isEmoticon: Bool = false {
        didSet {
            let imgName = isEmoticon ? "compose_keyboardbutton_background" : "compose_emoticonbutton_background"
            emoticonButton.setImage(UIImage(named: imgName), forState: .Normal)
            emoticonButton.setImage(UIImage(named: "\(imgName)_highlighted"), forState: .Highlighted)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置界面
    private func setupUI() {
        // 添加控件
        let pictureButton = addChildButton("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButton("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButton("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButton("compose_emoticonbutton_background",type: .Add)
        // 添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton)
        }
        
        emoticonButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton.snp_trailing)
        }
        
    }
    
    // MARK: - 添加按钮公共方法
    private func addChildButton(imgName: String, type: SHComposeToolbarViewType) -> UIButton {
        let button = UIButton(backgroundImgName: "compose_toolbar_background", imgName: imgName, isHighlighted: true, target: self, action: "didClickButton:")
        // rawValue 通过rawValue 把枚举名 转成枚举int数值
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Highlighted)
        button.tag = type.rawValue
        addSubview(button)
        
        return button
    }
    
    // MARK: - 按钮监听
    @objc private func didClickButton(sender: UIButton) {
        // 执行闭包
        closure?(type: SHComposeToolbarViewType(rawValue: sender.tag)!)
    }
    
}
